package cn.dsscm.service.impl;

import cn.dsscm.dao.ProductMapper;
import cn.dsscm.pojo.Product;
import cn.dsscm.service.ProductService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

@Service("productService")
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductMapper productMapper;

    @Override
    public Product getProductById(Integer id) throws Exception {
        return productMapper.getProductById(id);
    }

    @Override
    public int getProductCount(String name, Integer categoryLevel1Id,
                               Integer categoryLevel2Id, Integer categoryLevel3Id) throws Exception {
        return productMapper.getProductCount(name, categoryLevel1Id, categoryLevel2Id, categoryLevel3Id);
    }

    @Override
    @Transactional
    public boolean add(Product product) throws Exception {
        int result = productMapper.add(product);
        return result > 0;
    }

    @Override
    public List<Product> getProductList(String name, Integer categoryLevel1Id,
                                        Integer categoryLevel2Id, Integer categoryLevel3Id) throws Exception {
        return productMapper.getProductList(name, categoryLevel1Id, categoryLevel2Id, categoryLevel3Id, null, null);
    }

    @Override
    public List<Product> getProductList(String name, Integer categoryLevel1Id,
                                        Integer categoryLevel2Id, Integer categoryLevel3Id,
                                        Integer offset, Integer pageSize) throws Exception {
        return productMapper.getProductList(name, categoryLevel1Id, categoryLevel2Id, categoryLevel3Id, offset, pageSize);
    }

    @Override
    @Transactional
    public boolean deleteProductById(Integer delId) throws Exception {
        int result = productMapper.deleteProductById(delId);
        return result > 0;
    }

    @Override
    @Transactional
    public boolean modify(Product product) throws Exception {
        int result = productMapper.modify(product);
        return result > 0;
    }

    @Override
    public int productNameIsExist(String name) throws Exception {
        return productMapper.productNameIsExist(name);
    }
}